//
//  CommentCell.swift
//  FM
//
//  Created by x on 2017/9/26.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class CommentCell: UITableViewCell {

    var comment:Comment? {
        didSet {
            iconHeadImage.sd_setImage(with: comment?.user?.avaterURL)
            nameLable.text = comment?.user?.nickname
            contentLable.text = comment?.content
            timeLable.text = comment?.comment_time
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    func setupUI() -> Void {
        
        backgroundColor = UIColor.clear
        
        //1.添加子控件
        contentView.addSubview(iconHeadImage)
        contentView.addSubview(nameLable)
        contentView.addSubview(contentLable)
        contentView.addSubview(timeLable)
        contentView.addSubview(bgLbale)
        
        //2.布局子控件
        let dic = ["iconHeadImage":iconHeadImage,"nameLable":nameLable,"contentLable":contentLable,"timeLable":timeLable,"bgLbale":bgLbale] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[iconHeadImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[iconHeadImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconHeadImage]-5-[nameLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[nameLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconHeadImage]-5-[contentLable]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[nameLable]-10-[contentLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconHeadImage]-5-[timeLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[contentLable]-10-[timeLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[bgLbale]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[timeLable]-10-[bgLbale(0.5)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        contentView.addConstraints(cons)
    }
    
    /// 返回当前cell的高度
    ///
    /// - Returns: <#return value description#>
    func rowHeight(model:Comment) -> CGFloat {
        //1.从新走数据模型didSet方法,
        self.comment = model
        
        //2.刷新布局
        layoutIfNeeded()
        
        //3.返回高度
        return bgLbale.frame.maxY
    }
    
    //MARK: -- 懒加载
    private lazy var iconHeadImage:UIImageView = UIImageView.imageWith(radios: 25, imageName: "")
    
    private lazy var nameLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 12)
    
    private lazy var contentLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 12)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        return tt
    }()
    
    private lazy var timeLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.gray, fontSize: 10)
    
    private lazy var bgLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor(white: 0.9, alpha: 1.0))
        
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
